<template>
  <div class="search">
    <Card>
      <Form
        ref="searchForm"
        :model="searchForm"
        inline
        :label-width="75"
        class="search-form mb_10"
      >
        <Form-item label="会员名称">
          <Input
            type="text"
            v-model="searchForm.userName"
            placeholder="请输入会员名称"
            clearable
            style="width: 200px"
          />
        </Form-item>
        <Form-item label="PLUS类型">
          <Select
            v-model="searchForm.plusType"
            placeholder="请选择"
            clearable
            style="width: 200px"
          >
            <Option value="">全部</Option>
            <Option value="0">月卡</Option>
            <Option value="1">季卡</Option>
            <Option value="2">年卡</Option>
          </Select>
        </Form-item>
        <Form-item label="PLUS状态">
          <Select
            v-model="searchForm.isDue"
            placeholder="请选择"
            clearable
            style="width: 200px"
          >
            <Option value="">全部</Option>
            <Option value="1">已开通</Option>
            <Option value="0">已过期</Option>
            <Option value="2">即将到期</Option>
          </Select>
        </Form-item>
        <Form-item label="开通时间">
          <DatePicker
            v-model="searchForm.plusBeginDate"
            type="daterange"
            clearable
            placeholder="选择起始时间"
            style="width: 200px"
          ></DatePicker>
        </Form-item>
        <Button
          @click="handleSearch"
          type="primary"
          icon="ios-search"
          class="search-btn"
          >搜索</Button
        >
      </Form>
      <!-- <Row class="operation padding-row" v-if="getType !== 'ACTIVITY'">
        <Button @click="add" type="primary">添加优惠券</Button>
        <Button @click="delAll">批量关闭</Button>
      </Row> -->
      <Table
        v-if="refreshTable"
        :loading="loading"
        border
        :columns="columns"
        :data="data"
        ref="table"
        class="mt_10"
        @on-selection-change="changeSelect"
      >
        <template slot-scope="{ row }" slot="action">
          <Button type="info" size="small" @click="synchronization(row)"
            >同步
          </Button>
          <Button
            type="default"
            class="ml_5"
            size="small"
            @click="see(row, 'onlyView')"
            >查看
          </Button>
        </template>
      </Table>
      <Row type="flex" justify="end" class="mt_10">
        <Page
          :current="searchForm.pageNumber"
          :total="total"
          :page-size="searchForm.pageSize"
          @on-change="changePage"
          @on-page-size-change="changePageSize"
          :page-size-opts="[10, 20, 50]"
          size="small"
          show-total
          show-elevator
          show-sizer
        ></Page>
      </Row>
    </Card>
  </div>
</template>
<script>
import {
  getPlatformCouponList,
  updatePlatformCouponStatus,
  deletePlatformCoupon,
} from "@/api/promotion";

import { plusList } from "@/api/shopNewApi.js";
export default {
  name: "coupon",
  data() {
    return {
      loading: true, // 表单加载状态
      searchForm: {
        // 搜索框初始化对象
        userName: "",
        plusType: "", //plus状态
        isDue: "", //plus状态
        plusBeginDate: "", // 开通时间
        pageNumber: 1,
        pageSize: 10,
      },
      selectList: [], // 多选数据
      selectCount: 0, // 多选计数
      columns: [
        // 表头
        {
          type: "selection",
          width: 60,
          align: "center",
          fixed: "left",
        },
        {
          title: "昵称",
          key: "userName",
          minWidth: 100,
        },
        {
          title: "账户/手机号",
          key: "userMobile",
          minWidth: 100,
          tooltip: true,
        },
        {
          title: "PLUS开通时间",
          key: "plusBeginDate",
          width: 200,
        },

        {
          title: "到期时间",
          key: "plusEndDate",
          width: 200,
        },

        {
          title: "PLUS类型",
          key: "plusType",
          width: 150,
          render: (h, params) => {
              if (params.row.plusType === "0") {
              return h("Tag", { props: { color: "blue" } }, "月卡");
            } else if (params.row.plusType === "1") {
              return h("Tag", { props: { color: "geekblue" } }, "季卡");
            } else {
              return h("Tag", { props: { color: "purple" } }, "年卡");
            }
          },
        },
        {
          title: "PLUS状态",
          key: "isDue",
          width: 150,
          render: (h, params) => {
            if (params.row.isDue == "1") {
              return h("Tag", { props: { color: "blue" } }, "已开通");
            }else if (params.row.isDue == "0") {
              return h("Tag", { props: { color: "geekblue" } }, "已过期");
            }else if (params.row.isDue == "2") {
              return h("Tag", { props: { color: "blue" } }, "即将到期");
            }
          
          },
        },
        {
          title: "操作",
          slot: "action",
          align: "center",
          fixed: "right",
          width: 150,
        },
      ],
      data: [], // 表单数据
      total: 0, // 表单数据总数
      refreshTable: true, // 修改选中状态后刷新表格
      selectDate: [], //选中的信息
    };
  },
  props: {
    //优惠券类型 查询参数
    getType: {
      type: String,
      default: "",
    },
    promotionStatus: {
      type: String,
      default: "",
    },
    //已选择优惠券
    selectedList: {
      type: Array,
      default: () => {
        return [];
      },
    },
  },
  methods: {
    getList() {
      this.loading = true;
      if (this.searchForm.plusBeginDate) {
        var [searchTimeBegin, searchTimeEnd] = this.searchForm.plusBeginDate;
      }
      plusList({
        userName: this.searchForm.userName,
        plusType: this.searchForm.plusType,
        isDue: this.searchForm.isDue,
        searchTimeBegin: searchTimeBegin
          ? searchTimeBegin.Format("yyyy-MM-dd") + " " + "00:00:00"
          : null,
        searchTimeEnd: searchTimeEnd
          ? searchTimeEnd.Format("yyyy-MM-dd") + " " + "23:59:59"
          : null,
        page: this.searchForm.pageNumber,
        rows: this.searchForm.pageSize,
      }).then((res) => {
        this.loading = false;
        this.total = Number(res.result.total);
        this.data = res.result.records;
      });
      //  this.total
    },
    check() {
      // 选中的优惠券
      this.$emit("selected", this.selectList);
    },
    add() {
      // 跳转添加页面
      this.$router.push({ name: "add-platform-coupon" });
    },
    info(v) {
      // 查看优惠券
      this.$router.push({ name: "platform-coupon-info", query: { id: v.id } });
    },
    changePage(v) {
      // 改变页码
      this.searchForm.pageNumber = v;
      this.getList();
    },
    changePageSize(v) {
      // 改变页数
      this.searchForm.pageNumber = 1;
      this.searchForm.pageSize = v;
      this.getList();
    },
    handleSearch() {
      // 搜索
      this.searchForm.pageNumber = 1;
      this.searchForm.pageSize = 10;
      this.getList();
    },
    clearSelectAll() {
      // 清除选中状态
      this.$refs.table.selectAll(false);
    },
    /**
     * 选择优惠券
     */
    changeSelect(e) {
      console.log(9999);
      return;
      this.selectList = e;
      this.selectCount = e.length;
      if (this.getType === "ACTIVITY") this.check();
    },
    synchronization(v) {
      console.log("同步");
    },
    see(v, only) {
      //查看
      let data = { onlyView: true, id: v.userId ,type:'PLUS'};
      this.$router.push({ name: "member-detail", query: data });
    },
    delAll() {
      // 批量下架
      if (this.selectCount <= 0) {
        this.$Message.warning("您还未选择要下架的优惠券");
        return;
      }
      this.$Modal.confirm({
        title: "确认下架",
        content: "您确认要下架所选的 " + this.selectCount + " 条数据?",
        loading: true,
        onOk: () => {
          let ids = [];
          this.selectList.forEach(function (e) {
            ids.push(e.id);
          });
          let params = {
            couponIds: ids.toString(),
            promotionStatus: "CLOSE",
          };
          // 批量删除
          updatePlatformCouponStatus(params).then((res) => {
            this.$Modal.remove();
            if (res.success) {
              this.$Message.success("下架成功");
              this.clearSelectAll();
            }
          });
        },
      });
    },
  },
  mounted() {
    this.getList();
    // //如果作为组件方式，传入了类型值，则搜索参数附加类型
    // if (this.getType) {
    //   this.searchForm.getType = this.getType;
    //   this.columns.pop();
    // }
    // if (this.promotionStatus) {
    //   this.searchForm.promotionStatus = this.promotionStatus;
    //   this.columns.pop();
    // }
    // this.init();
  },
};
</script>
